package com.fenghongzhang.module_home.ui.recommend.movie

import androidx.fragment.app.viewModels
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import com.fenghongzhang.lib_base.core.BaseFragment
import com.fenghongzhang.module_home.HomeUIState
import com.fenghongzhang.module_home.R
import com.fenghongzhang.module_home.databinding.HomeFragmentRecommendBinding
import com.fenghongzhang.module_home.ui.recommend.RecommendIntent
import com.fenghongzhang.module_home.ui.recommend.RecommendUIState
import com.orhanobut.logger.Logger
import dagger.hilt.android.AndroidEntryPoint
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.fenghongzhang.module_home.ui.recommend.RecommendAdapter

/**
 * 本是推荐,
 * 内容是游戏
 */
@AndroidEntryPoint
class RecommendFragment : BaseFragment<HomeFragmentRecommendBinding, RecommendViewModel>(HomeFragmentRecommendBinding::inflate) {

    override val viewModel: RecommendViewModel by viewModels()

    private lateinit var recyclerView: RecyclerView
    private lateinit var adapter: RecommendAdapter // 假设有一个 RecommendAdapter

    override fun initView() {
        viewModel.sendEvent(RecommendIntent.LoadData)

        recyclerView = binding.recommendVideoRecyclerView
        recyclerView.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        adapter = RecommendAdapter() // 初始化适配器
        recyclerView.adapter = adapter

    }

    override fun initData() {
        collectState(viewModel.state) { state ->
            updateUI(state)
        }
    }

    private fun updateUI(state: RecommendUIState) {
        Logger.d(state.recommendMutilVideo)
        adapter.submitList(state.recommendMutilVideo) // 假设适配器有 submitList 方法
    }
}